package io.renren.modules.hospital.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.modules.hospital.entity.EmrGhInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import io.renren.modules.hospital.entity.EmrBlInfoEntity;
import io.renren.modules.hospital.service.EmrBlInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * @author starry
 * @email 2495646304@qq.com
 * @date 2024-01-29 18:00:41
 */
@RestController
@RequestMapping("hospital/emrblinfo")
@Api(value = "EMR病历接口", tags = "EMR病历接口")
public class EmrBlInfoController {
    @Autowired
    private EmrBlInfoService emrBlInfoService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    //@RequiresPermissions("hospital:emrblinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        List<EmrBlInfoEntity> page = emrBlInfoService.findBlList();

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{emrBlId}")
    @ApiOperation("信息")
    //@RequiresPermissions("hospital:emrblinfo:info")
    public R info(@PathVariable("emrBlId") Integer emrBlId) {
        EmrBlInfoEntity emrBlInfo = emrBlInfoService.getById(emrBlId);

        return R.ok().put("emrBlInfo", emrBlInfo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("新建一个病历")
    //病历和患者要保证唯一性
    public R save(@RequestBody EmrBlInfoEntity emrBlInfo) {
        //先查一查这个用户有没有病历
        EmrBlInfoEntity blInfo = emrBlInfoService.getBlByHzId(emrBlInfo.getEmrHzId());
        if (blInfo != null) {
            return R.ok("用户已存在病历").put("blInfo", blInfo);
        } else {
            emrBlInfoService.save(emrBlInfo);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    //@RequiresPermissions("hospital:emrblinfo:update")
    public R update(@RequestBody EmrBlInfoEntity emrBlInfo) {
        emrBlInfoService.updateById(emrBlInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    //@RequiresPermissions("hospital:emrblinfo:delete")
    public R delete(@RequestBody Integer[] emrBlIds) {
        emrBlInfoService.removeByIds(Arrays.asList(emrBlIds));

        return R.ok();
    }

    /**
     * 根据患者id查找病历
     *
     * @param hzId 患者编号
     * @return 病历实体
     */
    @PostMapping("/getBlByHzId")
    @ApiOperation("根据患者id查找病历")
    public R getBlByHzId(Integer hzId) {
        EmrBlInfoEntity blInfoEntity = emrBlInfoService.getBlByHzId(hzId);
        return R.ok().put("blEntity", blInfoEntity);
    }


    /**
     * 更新病历
     */
    @PostMapping("updateTime")
    @ApiOperation("更新病历节点")
    public R updateTime(Integer blId, String time) {
        emrBlInfoService.updateTime(blId, time);
        return R.ok();
    }
}
